package com.ch.engine.daily.bean;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * bean生命周期
 *
 * @author wangchunhui
 * @date 2025/7/29 9:29
 */
@Component
public class FullLifecycleProcessor implements InstantiationAwareBeanPostProcessor {

    private static final String BEAN_MAME = "fullLifecycleBean";

    @Override
    public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
        if (BEAN_MAME.equals(beanName)) {
            System.out.println("🔍 1.1 postProcessBeforeInstantiation()");
        }
        return null;
    }

    @Override
    public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
        if (BEAN_MAME.equals(beanName)) {
            System.out.println("🔍 3.1 postProcessAfterInstantiation()");
        }
        // 返回 true 表示继续进行属性注入
        return true;
    }

    @Override
    public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
        if (BEAN_MAME.equals(beanName)) {
            System.out.println("🔍 4.1 postProcessProperties()");
        }
        return pvs;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (BEAN_MAME.equals(beanName)) {
            System.out.println("🛠️ 6.1 postProcessBeforeInitialization()");
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (BEAN_MAME.equals(beanName)) {
            System.out.println("🛠️ 8.1 postProcessAfterInitialization()");
        }
        return bean;
    }
}
